
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/*
 * GUI.java
 *
 * Created on 14/06/2012, 17:50:45
 */
/**
 *
 * @author Paolo
 */
public class GUI extends javax.swing.JFrame {

    HashMap<String, ComunicationUniCast> pc = new HashMap<String, ComunicationUniCast>();
    ArrayList<String> nomeDosProcessos = new ArrayList<String>();
    Distributor d;
    Process p;
    Thread t;
    HashMap<String, ServerComunication> ps;

    /**
     * Creates new form GUI
     */
    public GUI() {
        // coloca o nome dos processos
        for (int i = 1; i <= 4; i++) {
            nomeDosProcessos.add(i + "");
        }
        nomeDosProcessos.add("nodo");
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jButton5 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jTextPane1);

        jButton5.setText("Passo");
        jButton5.setFocusable(false);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Distribuidor");
        jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButton1MouseClicked(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Processo");
        jRadioButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jRadioButton2MouseClicked(evt);
            }
        });

        buttonGroup2.add(jRadioButton3);
        jRadioButton3.setSelected(true);
        jRadioButton3.setText("Iniciador");

        buttonGroup2.add(jRadioButton4);
        jRadioButton4.setText("Outro");
        jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton4ActionPerformed(evt);
            }
        });

        jButton1.setText("Configurar conexões");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2)
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton3)
                            .addComponent(jRadioButton4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)))
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButton3)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jRadioButton4)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 541, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if (jRadioButton1.isSelected()) {
            d.passar();
        } else {
            p.passar();
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed


        if (jRadioButton1.isSelected()) {
            //Distribuidor
            pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));
            pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
            pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
            pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
            pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
            pc.put("prox", new ComunicationUniCast(jTextPane1, 8010, jTextField1.getText()));

            nomeDosProcessos.add("prox");

            if (jRadioButton3.isSelected()) {
                //iniciador
                d = new Distributor(jTextPane1, 0, 8000, pc, nomeDosProcessos);

            } else {
                //outros
                d = new Distributor(jTextPane1, 1, 8000, pc, nomeDosProcessos);
            }
            t = new Thread(d);
            t.start();
            jButton1.setVisible(false);

        } else if (jRadioButton2.isSelected()) {

            //processo
            if (jRadioButton3.isSelected()) {
                //Normal
                if (jTextField1.getText().equalsIgnoreCase("1")) {
                    pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
                    pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
                    pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));

                    p = new Process(jTextPane1, jTextField1.getText(), 0, 8001, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);

                } else if (jTextField1.getText().equalsIgnoreCase("2")) {
                    pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
                    pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
                    pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));


                    p = new Process(jTextPane1, jTextField1.getText(), 0, 8002, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);

                } else if (jTextField1.getText().equalsIgnoreCase("3")) {

                    pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
                    pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
                    pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));


                    p = new Process(jTextPane1, jTextField1.getText(), 0, 8003, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);

                } else if (jTextField1.getText().equalsIgnoreCase("4")) {
                    pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
                    pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
                    pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));


                    p = new Process(jTextPane1, jTextField1.getText(), 0, 8004, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);
                }

            } else if (jRadioButton4.isSelected()) {
                //Bizantino
                if (jTextField1.getText().equalsIgnoreCase("1")) {
                    pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
                    pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
                    pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));

                    p = new Process(jTextPane1, jTextField1.getText(), 1, 8001, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);

                } else if (jTextField1.getText().equalsIgnoreCase("2")) {
                    pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
                    pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
                    pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));

                    p = new Process(jTextPane1, jTextField1.getText(), 1, 8002, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);

                } else if (jTextField1.getText().equalsIgnoreCase("3")) {
                    pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
                    pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
                    pc.put("4", new ComunicationUniCast(jTextPane1, 8004, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));
 p = new Process(jTextPane1, jTextField1.getText(), 1, 8003, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);

                } else if (jTextField1.getText().equalsIgnoreCase("4")) {
                    pc.put("1", new ComunicationUniCast(jTextPane1, 8001, "localhost"));
                    pc.put("2", new ComunicationUniCast(jTextPane1, 8002, "localhost"));
                    pc.put("3", new ComunicationUniCast(jTextPane1, 8003, "localhost"));
                    pc.put("nodo", new ComunicationUniCast(jTextPane1, 8000, "localhost"));

                    p = new Process(jTextPane1, jTextField1.getText(), 1, 8004, pc, nomeDosProcessos);
                    t = new Thread(p);
                    t.start();
                    jButton1.setVisible(false);
                }
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jRadioButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton1MouseClicked
        jRadioButton3.setText("Iniciador");
        jRadioButton4.setText("Outro");
    }//GEN-LAST:event_jRadioButton1MouseClicked

    private void jRadioButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton2MouseClicked
        jRadioButton3.setText("Normal");
        jRadioButton4.setText("Bizantino");
    }//GEN-LAST:event_jRadioButton2MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(final String args[]) {

        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new GUI().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton5;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
}
